package org.reborn.engine.render.util

import _root_.javax.media.opengl.GL
import _root_.javax.media.opengl.GL._
import _root_.javax.media.opengl.GL2
import _root_.javax.media.opengl.GL2._

import org.reborn.engine.model.mesh.Mesh
import org.reborn.engine.model.mesh.VertexData
import org.reborn.engine.enums._
import ArrayType._
import FaceMode._
import UpdateFrequency._

import java.nio.Buffer



trait RenderMesh{
  def register(gl:GL2,mesh:Mesh,updateFreq:UpdateFrequency):Int;
  def unregister(gl:GL2,meshID:Int):Unit;
  def render(gl:GL2,id:Int):Unit;
  def update(gl:GL2,id:Int,newMesh:Mesh):Boolean;
  def updateArrayData(gl:GL2,id:Int,fromMesh:Mesh,aType:ArrayType):Boolean;
  def updateFaceData(gl:GL2,id:Int,fromMesh:Mesh):Boolean;
  
  protected def getFaceMode(mesh:Mesh):Int = {
    mesh.getFaceMode match{
	  case Point => GL_POINTS
	  case Line => GL_LINES
	  case Triangle => GL_TRIANGLES
	  case Quad => GL_QUADS
	}
  }
}

trait RenderMeshSupport{
  protected var supported:Option[Boolean] = None
  protected val functionList:List[String];
  
  def supportsMeshRenderMethod(gl:GL2):Boolean = {
    if(supported.isEmpty){
      supported = Some(functionList.forall((function) => 
        if(!gl.isFunctionAvailable(function)){
          println(function+" is not supported by your video card..")
          false
        }
        else
          true
      ))
    }
    supported.get
  }
  
  def createRenderer:RenderMesh;
}
